package cn.iocoder.yudao.module.jifenyuan.service.roomlog;

import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.roomlog.RoomLogWithInfoDO;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.jifenyuan.controller.app.roomlog.vo.*;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.roomlog.RoomLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.jifenyuan.convert.roomlog.RoomLogConvert;
import cn.iocoder.yudao.module.jifenyuan.dal.mysql.roomlog.RoomLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jifenyuan.enums.ErrorCodeConstants.*;

/**
 * 记分员 房间记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RoomLogServiceImpl implements RoomLogService {

    @Resource
    private RoomLogMapper roomLogMapper;
    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createRoomLog(AppRoomLogCreateReqVO createReqVO) {
        // 插入
        RoomLogDO roomLog = RoomLogConvert.INSTANCE.convert(createReqVO);
        roomLogMapper.insert(roomLog);
        // 返回
        return roomLog.getId();
    }

    @Override
    public void updateRoomLog(AppRoomLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateRoomLogExists(updateReqVO.getId());
        // 更新
        RoomLogDO updateObj = RoomLogConvert.INSTANCE.convert(updateReqVO);
        roomLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoomLog(Long id) {
        // 校验存在
        validateRoomLogExists(id);
        // 删除
        roomLogMapper.deleteById(id);
    }

    private void validateRoomLogExists(Long id) {
        if (roomLogMapper.selectById(id) == null) {
            throw exception(ROOM_LOG_NOT_EXISTS);
        }
    }

    @Override
    public RoomLogDO getRoomLog(Long id) {
        return roomLogMapper.selectById(id);
    }

    @Override
    public List<RoomLogDO> getRoomLogList(Collection<Long> ids) {
        return roomLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RoomLogDO> getRoomLogPage(AppRoomLogPageReqVO pageReqVO) {
        return roomLogMapper.selectPage(pageReqVO);
    }
    @Override
    public List<RoomLogWithInfoDO> getRoomLogByRoomId(Long roomId) {
        List<RoomLogDO> roomLogByRoomId = roomLogMapper.getRoomLogByRoomId(roomId);
        if(roomLogByRoomId!=null && roomLogByRoomId.size()>0){
            List<RoomLogWithInfoDO> roomLogWithInfoDOS = RoomLogConvert.INSTANCE.convertList03(roomLogByRoomId);
            HashSet<Long> userIds = new HashSet<>();
            for (RoomLogWithInfoDO roomLogWithInfoDO : roomLogWithInfoDOS) {
                userIds.add(roomLogWithInfoDO.getFromUser());
                userIds.add(roomLogWithInfoDO.getToUser());
            }
            List<Long> userIdList = new ArrayList<>();
            userIdList.addAll(userIds);
            List<MemberUserRespDTO> users = memberUserApi.getUsers(userIdList);

            for (RoomLogWithInfoDO roomLogWithInfoDO : roomLogWithInfoDOS) {
                MemberUserRespDTO fromUserRespDTO = users.stream().filter(i -> i.getId().equals(roomLogWithInfoDO.getFromUser())).findFirst().orElse(null);
                if(fromUserRespDTO!=null){
                    roomLogWithInfoDO.setFromNickname(fromUserRespDTO.getNickname());
                    roomLogWithInfoDO.setFromAvatar(fromUserRespDTO.getAvatar());
                }
                MemberUserRespDTO toUserRespDTO = users.stream().filter(i -> i.getId().equals(roomLogWithInfoDO.getToUser())).findFirst().orElse(null);
                if(toUserRespDTO!=null){
                    roomLogWithInfoDO.setToNickname(toUserRespDTO.getNickname());
                    roomLogWithInfoDO.setToAvatar(toUserRespDTO.getAvatar());
                }
            }
            return roomLogWithInfoDOS;
        }


        return null;
    }


    @Override
    public List<RoomLogDO> getRoomLogList(AppRoomLogExportReqVO exportReqVO) {
        return roomLogMapper.selectList(exportReqVO);
    }

}
